package com.e.takeaway.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.takeaway.R;
import com.e.takeaway.po.Food;
import com.squareup.picasso.Picasso;

import java.security.cert.CertificateRevokedException;
import java.util.List;

public class PayAdapter extends BaseAdapter
{
    private List<Food> foods;
    private Context ctr;
    private LayoutInflater inflater;

    public PayAdapter(Context context,List<Food> list){
        ctr = context;
        this.foods = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return foods.size();
    }

    @Override
    public Object getItem(int i) {
        return foods.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Food food = foods.get(i);
        PayAdapter.ViewHolder viewHolder = null;
        if (view == null){
            view = inflater.inflate(R.layout.item_pay,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.food_img = view.findViewById(R.id.py_im);
            viewHolder.food_name = view.findViewById(R.id.py_name);
            viewHolder.food_price = view.findViewById(R.id.py_price);
            viewHolder.food_number = view.findViewById(R.id.py_number);
            view.setTag(viewHolder);
        }else{
            viewHolder = (PayAdapter.ViewHolder)view.getTag();
        }
        viewHolder.food_name.setText(food.getFoodName());
        viewHolder.food_price.setText("￥"+food.getSum());
        viewHolder.food_number.setText(food.getNumber().toString());
        Picasso.with(ctr)
                .load(food.getFoodImg())
                .placeholder(viewHolder.food_img.getDrawable())
                .error(R.drawable.lunbo1)
                .into(viewHolder.food_img);
        return view;
    }



    static class ViewHolder{
        public ImageView food_img;
        public TextView food_name;
        public TextView food_price;
        public TextView food_number;

    }
}
